#include "StratGen.h"
using namespace BWAPI;
using namespace std;
#include <string>

StratGen::StratGen(void)
{
	more = true;
}

StratGen::~StratGen(void)
{
	for(map<string, vector<string>*>::iterator it = strategy.begin();
		it != strategy.end(); it++)
		delete[] (*it).second;
}
void StratGen::setEnemy(vector<string> _targets)
{
	for(vector<string>::iterator it=_targets.begin();it!=_targets.end();it++)
	{	
		if((UnitTypes::getUnitType(*it)).isFlyer())
			enemyAir.push_back(*it);
		else
			enemyGround.push_back(*it);
	}
}


void StratGen::addunits(std::vector<std::string> unitTypes)
{

	for(vector<string>::iterator it=unitTypes.begin();it!=unitTypes.end();it++)
	{
	vector<string>* v = new vector<string>;
	string unit = (*it);
	if((UnitTypes::getUnitType(unit)).isFlyer())
	{
		for(vector<string>::iterator it=enemyAir.begin();it!=enemyAir.end();it++)
			(*v).push_back(*it);
		for(vector<string>::iterator it=enemyGround.begin();
			it!=enemyGround.end();it++)
			(*v).push_back(*it);
	}
	else
	{
		for(vector<string>::iterator it=enemyGround.begin();
			it!=enemyGround.end();it++)
			(*v).push_back(*it);
		for(vector<string>::iterator it=enemyAir.begin();it!=enemyAir.end();it++)
			(*v).push_back(*it);
	}
	strategy[unit] =v;
	itera[unit] = (*v).begin();
	}
}

bool StratGen::isMore(){return more;}
void StratGen::reset(){more = true;}

map<string, vector<string>*> & StratGen::getNextStrat()
{
	if((enemyAir.size() + enemyGround.size()) < 2)
		more = false;
	if(more)
	{
		
		map<string,vector<string>::iterator>::iterator main = itera.begin();
		while(main != itera.end())
		{
			string name = (*main).first;
			vector<string>* vec = ((strategy.find(name)->second));
			if((*main).second == (*vec).end())
			{
				(*main).second = (*vec).begin();
				main++;
			}
			else
			{
				vector<string>::iterator temp = (*main).second;
				(*main).second++;
				if(main->second != vec->end())
				{
					(*temp).swap(*(*main).second);
					return strategy;
				}
			}
		}
		more = false;

	}
	return strategy;
}

